// <copyright file="InterpolationFieldControl.cs" company="MetricGR">
// Copyright MetricGR. All rights reserved.
// </copyright>
// <author>MAGNA\Andtheflux</author>
// <date>2015-07-24 13:40:28Z</date>
namespace MetricGR.Interpolation.Components
{
    using System;
    using System.Globalization;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;

    /// <summary>
    /// Control for field type InterpolationField
    /// </summary>  
    [Guid("dda507ca-caeb-42b9-8977-a67d21b2ab68")]
    public class InterpolationFieldControl : BaseFieldControl
    {
        protected override string DefaultTemplateName
        {
            get
            {
                return "MetricGR_Interpolation_InterpolationField_RenderingTemplate";
            }
        }

        public override object Value
        {
            get
            {
                string value = InterpolationFieldControl.GetItemFieldInterpolatedString(SPContext.Current.Site, SPContext.Current.Web, SPContext.Current.List, this.ItemContext.ListItem, this.Field);
                return value;
            }
            set
            {
                base.Value = value;
            }
        }

        public static string GetItemFieldInterpolatedString(SPSite site, SPWeb web, SPList list, SPListItem item, SPField field)
        {
            if (field == null)
            {
                // ERROR: no field given
                return string.Empty;
            }
            string json = field.GetProperty(Common.FieldInterpolationProperty);
            if (string.IsNullOrEmpty(json))
            {
                // ERROR: there is no interpolation string property in this field
                return string.Empty;
            }
            InterpolationFieldObject raterBBTObj = new InterpolationFieldObject(json);

            string value = Interpolation.ToString(raterBBTObj.InterpolationString, site, web, list, item);
            return value;
        }
    }
}

